#include "DataCommon.h"

DataCommon::DataCommon(QObject *parent)
    :QObject(parent)   
{

    createObject();
    connectObject();
    startObject();
}

DataCommon::~DataCommon()
{
    basicInfoModuleThread->quit();
    basicInfoModuleThread->wait();
    commonInfoModuleThread->quit();
    commonInfoModuleThread->wait();
    basicInfoModuleThread->deleteLater();
    commonInfoModuleThread->deleteLater();
}

void DataCommon::createObject()
{
    basicInfoModule = BasicInfoModule::instance();
    commonInfoModule = CommonInfoModule::instance();
    dataCollection = DBusModule::instance();
    cacheModule = CacheModule::instance();
    transmitModule = TransmitModule::instance();
    confModule = ConfModule::instance();

    basicInfoModuleThread = new QThread(this);
    commonInfoModuleThread = new QThread(this);

    basicInfoModule->moveToThread(basicInfoModuleThread);
    commonInfoModule->moveToThread(commonInfoModuleThread);
}

void DataCommon::connectObject()
{

    //  connect update property
    connect(confModule,&ConfModule::sendConf,basicInfoModule,&BasicInfoModule::updateProperty);
    connect(confModule,&ConfModule::sendConf,commonInfoModule,&CommonInfoModule::updateProperty);
    connect(confModule,&ConfModule::sendConf,dataCollection,&DBusModule::updateProperty);
    connect(confModule,&ConfModule::sendConf,cacheModule,&CacheModule::updateProperty);
    connect(confModule,&ConfModule::sendConf,transmitModule,&TransmitModule::updateProperty);
    connect(confModule,&ConfModule::sendConf,confModule,&ConfModule::updateProperty);

    connect(dataCollection,&DBusModule::saveUploadState,confModule,&ConfModule::writeConf);
    //  save info
    connect(dataCollection, &DBusModule::saveInfo, cacheModule, &CacheModule::recvUploadInfo);
    connect(basicInfoModule, &BasicInfoModule::sendInfo, cacheModule, &CacheModule::recvUploadInfo);
    connect(commonInfoModule, &CommonInfoModule::sendInfo, cacheModule, &CacheModule::recvCommonInfo);
    //  save tid
    connect(dataCollection, &DBusModule::saveTid, cacheModule, &CacheModule::recvTidInfo);
    //  check tid
    connect(dataCollection, &DBusModule::checkTid, cacheModule, &CacheModule::checkTidInfo,Qt::ConnectionType::DirectConnection);
    //  upload info
    connect(transmitModule, &TransmitModule::uploadInfoSignal, cacheModule, &CacheModule::recvUploadSignal);
    connect(transmitModule, &TransmitModule::reuploadInfoSignal, cacheModule, &CacheModule::recvReuploadSignal);

    connect(cacheModule, &CacheModule::sendUploadInfo, transmitModule, &TransmitModule::uploadInfo);
    connect(cacheModule, &CacheModule::sendReuploadInfo, transmitModule, &TransmitModule::reuploadInfo);

    connect(transmitModule, &TransmitModule::retUploadState, cacheModule, &CacheModule::recvUploadInfoAndState);
    connect(transmitModule, &TransmitModule::retReuploadState, cacheModule, &CacheModule::recvReuploadInfoAndState);
    //  start connect
    connect(this, &DataCommon::controlCollectInfo, basicInfoModule, &BasicInfoModule::dealInfo);
    connect(this, &DataCommon::controlCollectInfo, commonInfoModule, &CommonInfoModule::dealInfo);
}

void DataCommon::startObject()
{
    basicInfoModuleThread->start();
    commonInfoModuleThread->start();
    confModule->readConf(QString()+gs_cacheFolderPath+gs_confFileName);
    emit controlCollectInfo();
}
